package com.tuomaxu;

import com.tuomaxu.pojo.Mapper;
import com.tuomaxu.thread.MyRequestProcessor;
import com.tuomaxu.utils.ServerXmlParserUtil;
import com.tuomaxu.utils.ThreadPoolUtil;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.*;

public class Bootstrap {

    public static void main(String[] args) {
        System.out.println("Minicat init>>>>>>>>>>>");
        Bootstrap bootstrap = new Bootstrap();
        try {
            bootstrap.run_v4();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 作业实现
     */

    public void run_v4() throws Exception {
        //1,解析server.xml配置文件初 返回mapper对象
        Mapper mapper = ServerXmlParserUtil.parser("server.xml");

        //2，创建线程池
        ThreadPoolExecutor threadPoolExecutor = ThreadPoolUtil.create();

        //3,监听端口
        ServerSocket serverSocket = new ServerSocket(ServerXmlParserUtil.port);

        //4,处理请求
        while (true){
            Socket socket = serverSocket.accept();
            MyRequestProcessor myRequestProcessor = new MyRequestProcessor(socket,mapper);
            threadPoolExecutor.execute(myRequestProcessor);
        }
    }
}
